package com.example.ai.view


import android.media.AudioAttributes
import android.media.MediaPlayer
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.core.graphics.alpha
import androidx.core.graphics.blue
import androidx.core.graphics.green
import androidx.core.graphics.red
import com.example.ai.R
import com.example.ai.model.data.Identity
import com.example.ai.model.data.MessageData
import java.io.IOException

/**
 * Created by lijinxi on 2024/3/7.
 * @Description:
 */
@Composable
fun MessageItem(
    messageData: MessageData,
    index: Int,
    loadAudioUrl: (index: Int) -> Unit,
    hasAudioIcon: Boolean = false,
    loadTalkId: ((index: Int) -> Unit)? = null,
    loadTalkUrl: ((index: Int) -> Unit)? = null
) {
    val other by remember {
        mutableStateOf(messageData.identity == Identity.OTHER)
    }

    val alignment = if (other) {
        Arrangement.Start
    } else {
        Arrangement.End
    }
    val backgroundColor = if (other) {
        val mColor = android.graphics.Color.parseColor("#c0c0c0")
        Color(red = mColor.red, green = mColor.green, blue = mColor.blue, alpha = mColor.alpha)
    } else {
        val mColor = android.graphics.Color.parseColor("#688dd8")
        Color(red = mColor.red, green = mColor.green, blue = mColor.blue, alpha = mColor.alpha)
    }

    val textColor = if (other) Color.Black else Color.White

    //进行加载
    LaunchedEffect(key1 = messageData) {
        if (other && messageData.audio == null) {
            loadAudioUrl.invoke(index)
        }
        if (other && messageData.audio != null && messageData.talkID == null) {
            loadTalkId?.invoke(index)
        }
        if (other && messageData.audio != null && messageData.talkID != null && messageData.talkUrl == null) {
            loadTalkUrl?.invoke(index)
        }
    }

    //音频
    var isPlaying by remember { mutableStateOf(false) }

    val mediaPlayer = remember {
        MediaPlayer().apply {
            setAudioAttributes(
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .build()
            )
        }
    }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp, horizontal = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = alignment
    ) {
        Box(
            modifier = Modifier
                .clip(RoundedCornerShape(16.dp))
                .background(backgroundColor)
                .wrapContentSize()
        ) {
            Column(modifier = Modifier.wrapContentSize()) {
                Text(
                    text = messageData.text,
                    modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
                    color = textColor
                )
                if (other && hasAudioIcon && messageData.audio != null) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier.wrapContentSize()
                    ) {
                        IconButton(
                            onClick = {
                                if (isPlaying) {
                                    mediaPlayer.pause()
                                    isPlaying = !isPlaying
                                } else {
                                    mediaPlayer.reset()
                                    try {
                                        mediaPlayer.setDataSource(messageData.audio)
                                        mediaPlayer.setOnPreparedListener {
                                            mediaPlayer.start()
                                            isPlaying = !isPlaying
                                        }
                                        mediaPlayer.setOnCompletionListener {
                                            isPlaying = false
                                        }
                                        mediaPlayer.prepareAsync()
                                    } catch (e: IOException) {
                                        e.printStackTrace()
                                    }
                                }
                            }
                        ) {
                            if (!isPlaying) {
                                Icon(
                                    imageVector = Icons.Filled.PlayArrow,
                                    contentDescription = "Play"
                                )
                            } else {
                                Icon(
                                    painter = painterResource(id = R.drawable.pause),
                                    contentDescription = "Pause"
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}